<template>
  <div class="container">
    <nav-bar title="养老机构类型与区域分布情况"></nav-bar>
    <div class="chart-box chart1">
      <div class="chart-box-header">
        <pub-area
          :maxSelectLevel="3"
          @change="changeArea"
          customRightIcon
          label=""
          :isLink="false"
          valueIsLast
          ></pub-area>
        <div class="area-detail" @click="goDetailArea">
          区域分布情况
        </div>
      </div>
      <div class="chart-box-content">
        <div class="title">养老机构统计</div>
        <div class="bottom-box">
          <div class="left-box">
            <img src="./img/yljgzs.png" alt="">
            <div class="text">
              <div class="label">
                养老机构总数（个）
              </div>
              <div class="value">
                <count-to
                  :startVal="0"
                  :endVal="Number(state.detail.total)"
                  :decimals="0"
                  :duration="2000"
                ></count-to>
              </div>
            </div>
          </div>
          <div class="right-box">
            <div class="orgType" id="orgType"></div>
          </div>
        </div>
      </div>
    </div>
    <div class="chart-box chart2">
      <div class="chart-box-header">
        关键数据一览
      </div>
      <div class="chart-box-content">
        <div class="top">
          <div class="left" @click="go('/subsidy')">
            <div class="label">机构补贴金额（万元）</div>
            <div class="value">
              <count-to
                :startVal="0"
                :endVal="Number(state.detail.subsidy)"
                :decimals="2"
                :duration="2000"
              ></count-to>
            </div>
          </div>
          <div class="right"  @click="go('/startRating')">
            <div class="label">星级养老机构数（个）</div>
            <div class="value">
              <count-to
                :startVal="0"
                :endVal="Number(state.detail.rating1)"
                :decimals="0"
                :duration="2000"
              ></count-to>
            </div>
          </div>
        </div>
        <div class="bottom">
          <div class="item">
            <div class="label"><img src="./img/icon1.png" alt=""> 未评级机构数（个）</div>
            <div class="value">
              <count-to
                :startVal="0"
                :endVal="Number(state.detail.rating2)"
                :decimals="0"
                :duration="2000"
              ></count-to>
            </div>
          </div>
          <div class="item">
            <div class="label"><img src="./img/icon2.png" alt="">医养结合机构数（个)</div>
            <div class="value">
              <count-to
                :startVal="0"
                :endVal="Number(state.detail.medical)"
                :decimals="0"
                :duration="2000"
              ></count-to>
            </div>
          </div>
          <div class="item">
            <div class="label"><img src="./img/icon3.png" alt="">区域性养老服务中心(个)</div>
            <div class="value">
              <count-to
                :startVal="0"
                :endVal="Number(state.detail.area)"
                :decimals="0"
                :duration="2000"
              ></count-to>
            </div>
          </div>
          <div class="item">
            <div class="label"><img src="./img/icon4.png" alt="">床位总数(张)</div>
            <div class="value">
              <count-to
                :startVal="0"
                :endVal="Number(state.detail.bed)"
                :decimals="0"
                :duration="2000"
              ></count-to>
            </div>
          </div>
          <div class="item">
            <div class="label"><img src="./img/icon5.png" alt="">在住老人总数(人)</div>
            <div class="value">
              <count-to
                :startVal="0"
                :endVal="Number(state.detail.elder)"
                :decimals="0"
                :duration="2000"
              ></count-to>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { useUserStore } from '@/pinia/modules/user'
import { CountTo } from 'vue3-count-to'
import * as echarts from 'echarts'
import { ref, reactive, onBeforeMount,onMounted,nextTick,onBeforeUnmount } from 'vue'
import { get_pension_analysis } from '@/api/elderDistribution'
import { appCurrentState } from '@/pinia/modules/app.js'
import { formatDictCode } from '@/utils/format.js'
import { useRouter } from 'vue-router'
const active = ref(0);
const router = useRouter()
const state = reactive({
  searchParams: {
    areaCode:useUserStore().userLastArea
  },
  detail: {},
  myChart:null
})

const getElderDetail = async () => {
  let res = await get_pension_analysis(state.searchParams)
  if (res.succeed) {
    state.detail = res.data
    nextTick(() => {
      initChartOrgType()
    })
  }
}

const changeArea = val => {
  state.searchParams.areaCode = val
  getElderDetail()
}

// 年龄分析
const  initChartOrgType = async () => {
  state.myChart = echarts.init(document.getElementById('orgType'))
  const data = [
          { value: state.detail.orgType1, name: '公办' },
          { value: state.detail.orgType2, name: '民办' },
          { value: state.detail.orgType3, name: '公建民营' }

        ]
  const option = {
    legend: {
      bottom: '2%',
      left: 'center',
      orient: 'horizontal',
      icon: 'rect'
    },
    toolbox: {
      show: false,
    },

      tooltip: {
      trigger: 'item',
      formatter: '{b}: {c}个',
      backgroundColor: 'rgba(47,37,198,0.75)',
      borderColor :'#2880FF',
        borderWidth: 1,
        textStyle:{
          color  :'#fff'
        }
    },
    series: [
      {
        name: 'Nightingale Chart',
        type: 'pie',
        radius: ['40%','60%'],
        center: ['50%', '40%'],
        roseType: 'area',
        label: {
          show: false,
          formatter: '{c}',
        },
        labelLine: {
            normal: {
                length: 10, //第一条线
                length: 5, //第二条线
                lineStyle: {
                    width: 1, // 线条的宽度
                }
            }
        },
        selectedOffset :30,
        selectedMode: false,
        itemStyle: {
          borderColor: '#fff',
          borderWidth: 2
        },
        data: data
      }
    ]
  };

  state.myChart.setOption(option)
}

const go = (val) => {
  router.push(val)
}
const goDetailArea = () => {
  router.push('/pensionArea')
}
onBeforeUnmount(() => {
  state.myChart && state.myChart.dispose()
})
onMounted(() => {
  getElderDetail()
})
</script>
<style lang="less" scoped>
* {
  box-sizing: border-box;
}
.container {
  height: 100%;
  background-color: #f8f8f8;
  overflow-y: auto;

  .chart-box {
    // width: calc(100% - 80px);
    width: 100%;
    font-size: 30px;
    font-family: Microsoft YaHei;
    color: #000000;
    margin-top: 20px;
    background-color: #fff;

    .chart-box-header {
      display: flex;
      align-items: center;
      height: 90px;
      border-bottom: 1px solid #E6E6E6;
    }
  }

  .chart1 {
    height: 440px;
    .chart-box-header {
      justify-content: space-between;
      :deep(.van-cell) {
          width: 220px;
          height: 100%;
        }
      .area-detail {
        padding-right: 40px;
        font-weight: 400;
        color: #4375E4;
      }
    }

    .chart-box-content {
      .title {
        font-weight: bold;
        color: #000000;
        margin-top: 30px;
        margin-left: 40px;
        height: 30px;
        line-height: 30px;
      }

      .bottom-box {
        height: 290px;
        width: 100%;
        display: flex;
        padding: 0 40px;

        .left-box {
          display: flex;
          align-items: center;
          img {
            width: 97px;
            height: 97px;
            margin-right: 15px;
          }

          .text {
            font-family: PingFang SC;
            color: #333333;
            .label {
              font-size: 24px;
              margin-bottom: 30px;
              width: 220px;
            }

            .value {
              font-size: 36px;
            }
          }
        }

        .right-box {
          flex: 1;
          display: flex;
          justify-content: space-evenly;
          // padding: 20px 0 40px;
          font-size: 24px;
          font-family: Microsoft YaHei;
          font-weight: 400;
          color: #000033;

          .orgType {
            width: 100%;
            height: 290px;
          }
        }
      }
    }
  }

  .chart2 {
    height: 820px;
    .chart-box-header {
      padding-left: 40px;
      font-weight: bold;
    }

    .chart-box-content {
      height: 730px;
      padding: 0 40px;

      .top {
        margin: 26px 0 28px;
        display: flex;
        justify-content: space-between;

        .left,.right {
          width: 323px;
          height: 126px;
          padding: 16px 0 16px 30px;
          font-size: 24px;
          font-family: PingFang SC;
          color: #3852A9;
          background-image: url(./img/health.png);
          background-size: 100% 126px;
          background-repeat: no-repeat;

          .label {
            margin-bottom: 20px;
          }

          .value {
            font-size: 36px;
          }
        }

        .right {
          color: #219421;
          background-image: url(./img/no-health.png);
        }
      }

      .bottom {
        height: 550px;

        .item {
          height: 110px;
          display: flex;
          justify-content: space-between;
          align-items: center;
          border-bottom: 1px dashed #959595;
          .label {
            display: flex;
            align-items: center;

            img {
                width: 51px;
                height: 51px;
                margin-right: 28px;
            }
          }
        }
        .item:nth-child(5){
          border-bottom: none;
        }
      }
    }

  }
}
</style>
